package ufc.si.fbd.logica.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import ufc.si.fbd.logica.ConnectionFactory;
import ufc.si.fbd.logica.entidades.Evento;

public class EventoDAO {

    private Connection con;
    private ResultSet rs;
    private PreparedStatement ps;
    public void salvar(Evento evento) throws SQLException {
        con = ConnectionFactory.getConnection();
        String sql;
        if (evento.getId() == 0) {
            sql = "INSERT INTO eventos (descricao) VALUES (?)";
            ps = con.prepareStatement(sql);
            ps.setString(1, evento.getDescricao());
            ps.executeUpdate();
        } else {
            sql = "UPDATE eventos SET descricao = ? WHERE id = ?";
            ps = con.prepareStatement(sql);
            ps.setString(1, evento.getDescricao());
            ps.setInt(2, evento.getId());
            ps.executeUpdate();
        }
        con.close();
    }

    public List<Evento> listar() throws SQLException {
        con = ConnectionFactory.getConnection();
        String sql = "SELECT * FROM eventos";
        ps = con.prepareStatement(sql);
        rs = ps.executeQuery();
        List<Evento> eventos = new ArrayList<>();
        while (rs.next()) {
            int id = rs.getInt("id");
            String descricao = rs.getString("descricao");
            Evento evento = new Evento(id, descricao);
            eventos.add(evento);
        }
        con.close();
        return eventos;
    }
    
    public List<Evento> listar(String nome) throws SQLException {
        con = ConnectionFactory.getConnection();
        String sql = "SELECT * FROM eventos where descricao ilike ?";
        ps = con.prepareStatement(sql);
        nome = "%"+nome+"%";
        ps.setString(1,nome);
        rs= ps.executeQuery();
        List<Evento> eventos = new ArrayList<>();
        while (rs.next()) {
            int id = rs.getInt("id");
            String descricao = rs.getString("descricao");
            Evento evento = new Evento(id, descricao);
            eventos.add(evento);
        }
        con.close();
        return eventos;
    }

    public Evento find(int id) throws SQLException {
        con = ConnectionFactory.getConnection();
        String sql = "SELECT * FROM eventos WHERE id = ?";
        ps = con.prepareStatement(sql);
        ps.setInt(1, id);
        rs = ps.executeQuery();
        Evento evento = new Evento();
        while (rs.next()) {
            evento.setId(rs.getInt("id"));
            evento.setDescricao(rs.getString("descricao"));
        }
        con.close();
        return evento;
    }

    public void remover(int id) throws SQLException {
        con = ConnectionFactory.getConnection();
        String sql = "DELETE FROM eventos WHERE id = ?";
        ps = con.prepareStatement(sql);
        ps.setInt(1, id);
        ps.executeUpdate();
        con.close();
    }
}
